package com.woniuxy.background.service.impl;


import com.woniuxy.background.dao.RoleMapper;
import com.woniuxy.background.dao.RoleMenuMapper;
import com.woniuxy.background.pojo.Role;
import com.woniuxy.background.pojo.RoleMenuKey;
import com.woniuxy.background.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@Resource
public class RoleServiceImpl implements RolesService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Role> findByPage(Integer currentPage, Integer pageSize) {
        return roleMapper.selectByPage(currentPage,pageSize);
    }

    @Override
    public Role findRolesByName(String roleName) {
        return roleMapper.selectByName(roleName);
    }

    @Override
    public void saveRoles(Role role, String menuId) {
        //添加角色
        roleMapper.insertSelective(role); //如何获取到新添加这条角色数据的id
        Role nRole = roleMapper.selectByName(role.getRoleName());
        List<Integer> menuIds = Arrays.stream(menuId.split(",")).map(id->Integer.parseInt(id)).collect(Collectors.toList());
        List<RoleMenuKey> list = new ArrayList();
        for (int id : menuIds) {
            RoleMenuKey menusKey = new RoleMenuKey();
            menusKey.setMenuId(id);
            menusKey.setRoleId(nRole.getRoleId());
            list.add(menusKey);
        }
        roleMenuMapper.saveRolesMenus(list);
    }

    @Override
    public Role findRolesById(int roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public boolean checkRoleName(Role role) {
        Role dbRoles = roleMapper.selectByName(role.getRoleName());
        if(dbRoles == null){
            return false;
        }else{
            if(dbRoles.getRoleId().equals(role.getRoleId())){
                return false;
            }else{
                return true;
            }
        }
    }

    @Override
    public void updateRoles(Role role, String menuId) {
        //更新角色
        roleMapper.updateByPrimaryKeySelective(role);

        //更新权限数据，默认的权限先删除，在添加
        roleMenuMapper.deleteByRoleId(role.getRoleId());

        List<Integer> list = Arrays.stream(menuId.split(",")).map(mid->Integer.parseInt(mid)).collect(Collectors.toList());
        List<RoleMenuKey> rolesMenusKeys = new ArrayList<>();
        for (int mid : list) {
            RoleMenuKey rm = new RoleMenuKey();
            rm.setMenuId(mid);
            rm.setRoleId(role.getRoleId());
            rolesMenusKeys.add(rm);
        }
        roleMenuMapper.saveRolesMenus(rolesMenusKeys);
    }

    @Override
    public List<Role> selectRoles() {
        return roleMapper.selectRoles();
    }

    @Override
    public int deleteByPrimaryKey(Integer roleId) {
        return roleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public Role selectByPrimaryKey(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }


}
